/*************************************************
* Copyright(c) free                              *
* Beijing Jiaotong University                    *
* TEAM: Wasabi                                   *
* AUTHOR: Li Wei          			 *
* This file is used to init the windows,contains * 
* its panel,label,command,menu and the like.     * 
* Version 1.0 2011-01-6                          *
*************************************************/

#include <gtk/gtk.h>
#include "../include/callbacks.h"
#include "../include/interface.h"
#include "../include/logo.h"

GtkWidget * view1, * view2;	/* The first text editing area */
GtkTextBuffer * buffer1, *buffer2;	/* The first text buffer */
GtkClipboard * clipboard;/* Clipboard */
gboolean flag;/* Programe selection */
gboolean flagv;/* View selection */
GtkWidget * radio1, *radio2;
GtkWidget * radio3, *radio4;
GtkWidget * view_vbox, *view_hbox;
GtkWidget* frame1;
GtkWidget* frame2;
GtkWidget* frame11;
GtkWidget* frame22;
GtkWidget *scwin1, *scwin2;

static GtkItemFactoryEntry menu_items[] = {
     //Define menu
    {"/ 文件(_F) ", NULL, NULL, 0, "<Branch>"},
    {"/ 文件(_F) /打开", "<Control>O", on_file_open_activate,0,NULL},
    {"/ 文件(_F) /保存", "<Control>S", on_file_save_activate,0,NULL},
    {"/ 文件(_F) /退出", "<Control>Q", gtk_main_quit,0, NULL},
    {"/ 操作(_M) ", NULL, NULL, 0, "<Branch>"},
    {"/ 操作(_M) /注音", "<Control>P", on_zhuyin_activate,0,NULL},
    {"/ 操作(_M) /清屏", "<Control>R", on_reset_activate,0,NULL},
    {"/ 算法选择(_A) ", NULL, NULL, 0, "<Branch>"},
    {"/ 算法选择(_A) /最大正向分词", "<Control>F", on_fwd_activate,0,NULL},//"<RadioItem>"
    {"/ 算法选择(_A) /最大反向分词", "<Control>B", on_back_activate,0,NULL},
    {"/ 视图(_V) ", NULL, NULL, 0, "<Branch>"},
    {"/ 视图(_V) /垂直", "<Control>U", on_v_view_activate,0,NULL},
    {"/ 视图(_V) /水平", "<Control>H", on_h_view_activate,0,NULL},
    {"/ 帮助(_H) ", NULL, NULL, 0, "<Branch>"},
    {"/ 帮助(_H) /用户手册(_M)", NULL, on_man_activate,0,NULL},
    {"/ 帮助(_H) /关于(_A)", NULL, on_about_activate,0,NULL}
};


/***************************************************************************************************
 create_window : to init the window and its controls,like the menu,label,command.
 parameter : null.
 return type : GtkWidget * 
        value : a window pointer.
 detail descrpition : Draw window, establishing functional mapping, and returns window pointer.
****************************************************************************************************/
GtkWidget * create_window (void){
    GtkWidget * table;
    GtkWidget * window;
    GtkWidget * vbox;
    GtkWidget * boxm;
    GtkWidget * menubar;
    GtkAccelGroup * accel_group;
    GtkItemFactory * item_factory;
    gint n = sizeof(menu_items) / sizeof(menu_items[0]);
	
    GtkTextIter end1, end2;
    GtkWidget *frame3, *frame4, *frame5, *frame6;
    
    //Statement shaped containers
    GdkPixbuf *pixbuf;
    GtkWidget *image;
    GtkWidget * button0;
    GtkWidget * button1;
    GtkWidget * button2;
    GtkWidget * button3;
    GtkWidget * button4;	
    GSList * group1, *group2;
    GtkWidget * box, * box1, * box2;
	
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window),1000,600);
    gtk_window_set_title(GTK_WINDOW(window),"Wasabi--现代汉语拼音标注");
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
    //gtk_container_set_border_width(GTK_CONTAINER(window),10);

    //Create frame
    frame1 = gtk_frame_new("请在此窗口输入需要注音的文字：");
    frame11 = gtk_frame_new("请在此窗口输入需要注音的文字：");	
    frame2 = gtk_frame_new("此窗口输出完成注音的文字：");
    frame22 = gtk_frame_new("此窗口输出完成注音的文字：");
    frame3 = gtk_frame_new("操作选项：");
    frame4 = gtk_frame_new("中文分词算法选择：");
    frame5 = gtk_frame_new("视图模式选择：");
    frame6 = gtk_frame_new("");

    //Create shaped containers
    vbox = gtk_vbox_new(FALSE, 0);
    view_vbox = gtk_vbox_new(TRUE, 0);
    view_hbox = gtk_hbox_new(TRUE, 0);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    accel_group = gtk_accel_group_new();	
    item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);
    gtk_item_factory_create_items(item_factory, n, menu_items, NULL);
    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
    if (menubar)
	menubar = gtk_item_factory_get_widget (item_factory, "<main>");
    gtk_box_pack_start(GTK_BOX(vbox), menubar, 0, 0, 0);
    gtk_widget_show(menubar);

    table = gtk_table_new(80,51,FALSE);
    gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 2);
	
    pixbuf = gdk_pixbuf_new_from_inline (-1, logo_inline, TRUE, NULL);
    image = gtk_image_new_from_pixbuf(pixbuf);
    gtk_table_attach_defaults(GTK_TABLE(table),frame6,0,5,20,50);
    gtk_container_add(GTK_CONTAINER(frame6), image);

    scwin1 = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin1),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    view1 = gtk_text_view_new();
    /* buffer1 */
    buffer1 = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view1));
    //g_signal_connect(G_OBJECT(buffer1),"changed",
	//			G_CALLBACK(buffer1_length),NULL);
    gtk_text_buffer_get_end_iter(buffer1, &end1);
    //gtk_text_buffer_insert(buffer1, &end1, "欢迎使用wasabi！", -1);

    gtk_table_attach_defaults(GTK_TABLE(table),view_vbox,5,80,0,50);
    gtk_table_attach_defaults(GTK_TABLE(table),view_hbox,5,80,0,50);
    gtk_box_pack_start(GTK_BOX(view_vbox), frame1, TRUE, TRUE, 3);
    gtk_box_pack_start(GTK_BOX(view_hbox), frame11, TRUE, TRUE, 3);
    gtk_container_add(GTK_CONTAINER(frame1),scwin1);
    gtk_container_add(GTK_CONTAINER(scwin1),view1);

    scwin2 = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin2),GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    view2 = gtk_text_view_new();
    /* buffer2 */
    buffer2 = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view2));
    gtk_text_buffer_get_end_iter(buffer2, &end2);

    //gtk_text_buffer_insert(buffer2, &end2, "欢迎  使用  wasabi ！\nhuan1ying2  shi3yong4  wasabi ！\n", -1);
    gtk_box_pack_start(GTK_BOX(view_vbox), frame2, TRUE, TRUE, 3);
    gtk_box_pack_start(GTK_BOX(view_hbox), frame22, TRUE, TRUE, 3);
    gtk_container_add(GTK_CONTAINER(frame2),scwin2);
    gtk_container_add(GTK_CONTAINER(scwin2),view2);

    box = gtk_hbox_new(TRUE, 0);//false left alignment 
    gtk_table_attach_defaults(GTK_TABLE(table),frame3,0,5,0,1);
    gtk_container_add(GTK_CONTAINER(frame3),box);

    box1 = gtk_vbox_new(TRUE, 0);
    gtk_table_attach_defaults(GTK_TABLE(table),frame4,0,5,10,15);
    gtk_container_add(GTK_CONTAINER(frame4),box1);

    box2 = gtk_vbox_new(TRUE, 0);
    gtk_table_attach_defaults(GTK_TABLE(table),frame5,0,5,15,20);
    gtk_container_add(GTK_CONTAINER(frame5),box2);

    radio1 = gtk_radio_button_new_with_label(NULL, "最大正向分词 ");
    gtk_box_pack_start(GTK_BOX(box1), radio1, FALSE, FALSE, 5);
    g_signal_connect(G_OBJECT(radio1), "released",
				G_CALLBACK(on_radio_clicked),(gpointer)1);
	
    group1 = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio1));
    radio2 = gtk_radio_button_new_with_label(group1, "最大反向分词 ");
    gtk_box_pack_start(GTK_BOX(box1), radio2, FALSE, FALSE, 5);
    g_signal_connect(G_OBJECT(radio2), "released",
				G_CALLBACK(on_radio_clicked),(gpointer)2);

    /* Programe selection box */
    radio3 = gtk_radio_button_new_with_label(NULL, "垂直视图 ");
    gtk_box_pack_start(GTK_BOX(box2), radio3, FALSE, FALSE, 5);
    g_signal_connect(G_OBJECT(radio3), "released",
				G_CALLBACK(on_radio2_clicked),(gpointer)1);
	
    group2 = gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio3));
    radio4 = gtk_radio_button_new_with_label(group2, "水平视图 ");
    gtk_box_pack_start(GTK_BOX(box2), radio4, FALSE, FALSE, 5);
    g_signal_connect(G_OBJECT(radio4), "released",
				G_CALLBACK(on_radio2_clicked),(gpointer)2);

    button1 = gtk_button_new_with_label("注音");
    g_signal_connect(G_OBJECT(button1),"clicked",
				G_CALLBACK(on_button1_clicked),NULL);
    /* Open file or path */
    button2 = gtk_button_new_with_label("打开");
    g_signal_connect(G_OBJECT(button2),"clicked",
			G_CALLBACK(on_button2_clicked),NULL);
    button3 = gtk_button_new_with_label("保存");
    g_signal_connect(G_OBJECT(button3),"clicked",
				G_CALLBACK(on_button3_clicked),NULL);
    button4 = gtk_button_new_with_label("清屏");
    g_signal_connect(G_OBJECT(button4),"clicked",
			G_CALLBACK(on_button4_clicked),NULL);
    gtk_box_pack_start(GTK_BOX(box), button1, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(box), button2, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(box), button3, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(box), button4, FALSE, FALSE, 0);

    gtk_widget_show(vbox);	
    gtk_widget_show(table);
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(on_window_delete_event),NULL);
    gtk_widget_show_all(window);
    gtk_widget_hide(view_hbox);
    return window;
}
